package org.jeecgframework.web.system.controller.core;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.PlatformVersion;
import org.jeecgframework.web.system.service.PlatformVersionService;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.tcdev.service.PlatformConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;


/**
 * 系统软件版本维护
 * 
 * @author 钟杰宇
 * 
 */
@Controller
@RequestMapping("/platformVersionController")
public class PlatformVersionController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(PlatformVersionController.class);

	@Autowired
	private PlatformVersionService platformVersionService;

	@Autowired
	private SystemService systemService;

	private String message;



	/**
	 * 配置列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "platformVersion")
	public ModelAndView platformVersion() {
		return new ModelAndView("system/platformVersion/platformVersionList");
	}

	/**
	 * easyuiAjax表单请求
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 */
	@RequestMapping(params = "datagrid")
	public void datagrid(HttpServletRequest request,
			HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(PlatformVersion.class, dataGrid);
		this.platformVersionService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除配置信息
	 * 
	 * @param version
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(PlatformVersion version, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		version = platformVersionService.getEntity(PlatformVersion.class, version.getId());
		message = "版本: " + version.getVersion() + "被删除成功";
		platformVersionService.delete(version);
		systemService.addLog(message, Globals.Log_Type_DEL,
				Globals.Log_Leavel_INFO);
		
		return j;
	}

	/**
	 * 添加和更新配置信息
	 * 
	 * @param version
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(PlatformVersion version, HttpServletRequest request) {

		if (StringUtil.isEmpty(version.getId())) {

			List<PlatformVersion> versionList = platformVersionService.findHql("from PlatformVersion where type=" + version.getType() + " and version='" + version.getVersion() + "'");

			if(versionList != null && versionList.size() > 0){
				message = "版本: " + version.getVersion() + "的已存在";
			}else{

				if(1 == version.getIsable()){//是否发布，如果为是，那么就插入发布时间
					version.setReleaseTime(new Date());
				}

				platformVersionService.save(version);
				message = "版本: " + version.getVersion() + "被添加成功";
				systemService.addLog(message, Globals.Log_Type_INSERT,
						Globals.Log_Leavel_INFO);
			}
			
		}else{

			PlatformVersion t = platformVersionService.get(PlatformVersion.class,version.getId());

			try {
				MyBeanUtils.copyBeanNotNull2Bean(version, t);

				if(t.getReleaseTime() == null){

					if(1 == version.getIsable()){//是否发布，如果为是，那么就插入发布时间
						t.setReleaseTime(new Date());
					}

				}

				platformVersionService.saveOrUpdate(t);

				message = "版本: " + version.getVersion() + "被修改成功";

				systemService.addLog(message, Globals.Log_Type_UPDATE,
						Globals.Log_Leavel_INFO);

			} catch (Exception e) {

				message = "版本: " + version.getVersion() + "被修改失败";
				systemService.addLog(message, Globals.Log_Type_UPDATE,
						Globals.Log_Leavel_INFO);
				e.printStackTrace();
			}
		}
		AjaxJson j = new AjaxJson();
		j.setMsg(message);
		
		return j;
	}

	/**
	 * 添加和更新配置信息页面
	 * 
	 * @param platformVersion
	 * @param req
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(PlatformVersion platformVersion, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(platformVersion.getId())) {
			platformVersion = platformVersionService.getEntity(PlatformVersion.class,
					platformVersion.getId());
		}
		return new ModelAndView("system/platformVersion/platformVersion","platformVersion",platformVersion);
	}

}
